/**
 * Created by win8 on 2016-07-15.
 */
;
(function ($) {
    'use strict';

    var MsgBox = function (options) {
        this.settings = $.extend({}, MsgBox.defaults, options);
        this.init(options);
    };

    MsgBox.prototype = {

        /*
         * 初始化
         */
        init: function (options) {
            this.create();
        },

        /*
         * 创建元素
         */
        create: function () {
            $("body").find('.msg-wrap,.mask').remove();
            var header = (this.settings.showTitle) ? "<div class='msg-head'>" + this.settings.title + "</div>" : "";
            //var mask = "<div class='mask' style='display:block'></div>";
            var template = "<div class='msg-box'>" +
                header +
                "<div class='msg-body'>" + this.settings.msgContent + "</div>" +
                "<div class='msg-foot'></div>" +
                "</div>";

            var html = $('<div>').addClass('msg-wrap').append($(template));

            this.msgbox = $("body").append($(html),this.mask());

            // 设置ok按钮
            if ($.isFunction(this.settings.submit)) {
                this.submit();
            }

            // 设置cancel按钮
            if ($.isFunction(this.settings.cancel)) {
                this.cancel();
            }

            // 设置关闭按钮
            if (this.settings.close) {
                this.colseable();
            }
        },

        /*
         *submit
         */
        submit: function () {
            var _this = this,
                footer = this.msgbox.find('.msg-foot');

            $('<span>', {
                text: this.settings.buttons[1]
            }).on('click', function () {
                var okCallback = _this.settings.submit();
                if (okCallback == undefined || okCallback) {
                    _this.close();
                }
            }).addClass('msg-btn msg-submit').prependTo(footer);
        },

        mask: function () {
            var _this = this;

            return $('<div>').on('click', function () {
                _this.close();
            }).addClass('mask').css('display','block');
        },

        /*
         *cancel
         */
        cancel: function () {
            var _this = this,
                footer = this.msgbox.find('.msg-foot');
            $('<span>', {
                text: this.settings.buttons[0]
            }).on('click', function () {
                var cancelCallback = _this.settings.cancel();
                if (cancelCallback == undefined || cancelCallback) {
                    _this.close();
                }
            }).addClass('msg-btn msg-cancel').prependTo(footer);
        },

        /*
         *关闭按钮
         */
        colseable: function () {
            var _this = this,
                box = this.msgbox.find('.msg-wrap');
            $('<div>').on('click', function () {
                _this.close();
            }).addClass('msg-close').appendTo(box);
        },
        close: function () {
            $('.msg-wrap').remove();
            $('.mask').remove();
        }
    };

    MsgBox.defaults = {
        showTitle: false,//显示标题
        title: '提示',
        buttons: ['取消', '确定'],//按钮内容
        showSubmit: true,//显示确定按钮
        showCancel: true,//显示取消按钮
        close: true,//是否可关闭
        msgContent: '确定删除吗？',
        cancel: null,
        submit: null
    };

    $.msgbox = function (options) {
        return new MsgBox(options);
    };
})(Zepto);
